/**
 * Copyright (c) 2010 XXXXX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.edu.itba.ia2010.geneticos.motor.api.codecs;

import org.apache.commons.lang.Validate;

import ar.edu.itba.ia2010.geneticos.motor.api.Cromosoma;
import ar.edu.itba.ia2010.geneticos.motor.api.CromosomaCodec;
import ar.edu.itba.ia2010.geneticos.motor.api.Gen;

/**
 * Decodifica cromosomas codificados como bit (10010101) en numeros enteros.
 * 
 * @author Juan F. Codagnone
 * @since Mar 21, 2010
 */
public class LongBooleanCromosomaCodec implements CromosomaCodec<Boolean, Long> {
    
    /** @see CromosomaCodec#decode(Cromosoma) */
    public final Long decode(final Cromosoma<Boolean> cromosoma) {
        Validate.notNull(cromosoma);
    
        long i = 0;
        for(final Gen<Boolean> gen : cromosoma) {
            i  = i << 1;
            i = i | (gen.getValue() == Boolean.TRUE ? 1 : 0);
        }
        
        return i;
    }

}
